package net.xxcxcxcx.xc_enhancedmob.render.particle.summon;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.api.EnvironmentInterface;
import net.fabricmc.api.EnvironmentInterfaces;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.util.math.Vec3d;

import static net.xxcxcxcx.xc_enhancedmob.render.particle.ParticleHelper.customParticleList;

@EnvironmentInterfaces(value = {@EnvironmentInterface(value = EnvType.CLIENT, itf = ClientWorld.class)})
public class BaseSummonParticle implements CustomSummonParticle {
    public long time;
    public Vec3d pos;

    public BaseSummonParticle() {
        this(0, new Vec3d(0, 0, 0));
    }

    public BaseSummonParticle(long t, Vec3d pos) {
        time = t;
        this.pos = pos;
    }

    public BaseSummonParticle(PacketByteBuf buf) {
        read(buf);
    }

    @Override
    public void read(PacketByteBuf buf) {
        time = buf.readLong();
        double x = buf.readDouble();
        double y = buf.readDouble();
        double z = buf.readDouble();
        pos = new Vec3d(x, y, z);
    }

    //文件保存为NBT
    @Override
    public void write(PacketByteBuf buf) {
        buf.writeLong(time);
        buf.writeDouble(pos.x);
        buf.writeDouble(pos.y);
        buf.writeDouble(pos.z);
    }

    //自动生成CustomID
    @Override
    public int getID() {
        for (int i = 0; i < customParticleList.size(); i++) {
            if (customParticleList.get(i) == this.getClass()) {
                return i;
            }
        }
        return -1;
    }

    //每秒中要执行的粒子效果
    @Override
    @Environment(value = EnvType.CLIENT)
    public void Dotick(ClientWorld clientWorld) {
        clientWorld.addParticle(ParticleTypes.LAVA, pos.getX(), pos.getY(), pos.getZ(), clientWorld.random.nextGaussian() * 0.05, clientWorld.random.nextGaussian() * 0.05, clientWorld.random.nextGaussian() * 0.05);
    }

    //粒子效果还能否执行
    @Override
    @Environment(value = EnvType.CLIENT)
    public boolean CanUse(ClientWorld clientWorld) {
        return time >= clientWorld.getTime();
    }
}
